DECLARE @RegPathParams sysname
DECLARE @Arg sysname
DECLARE @Param sysname
DECLARE @MasterPath nvarchar(512)
DECLARE @n int

SET @n = 0
SELECT @RegPathParams=N'Software\Microsoft\MSSQLServer\MSSQLServer'+'\Parameters'
SET @Param = ''
WHILE(NOT @Param is null)
BEGIN
	SET @Param = null
	SET @Arg = 'SqlArg' + CONVERT(NVARCHAR, @n)
	EXEC master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE', @RegPathParams, @Arg, @Param OUTPUT
	IF(@Param like '-d%')
	BEGIN
		SET @Param = substring(@Param, 3, 255)
		SET @MasterPath = SUBSTRING(@Param, 1, len(@Param) - CHARINDEX('\', REVERSE(@Param)))
	END
	
	SELECT @n = @n + 1
END

SELECT @MasterPath AS [MasterDBPath]
